<?php

namespace App\Http\Controllers\Admin;

use App\Models\LmColumn;
use App\Models\LmSubject;
use App\Models\Photos;
use App\Models\QuestionType;
use App\Models\YtkAdmin;
use App\Models\YtkSquare;
use App\Models\YtkSquareCate;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;
use Stevenyangecho\UEditor\Lists;
use Stevenyangecho\UEditor\ListsQiniu;
use Stevenyangecho\UEditor\Uploader\UploadCatch;
use Stevenyangecho\UEditor\Uploader\UploadFile;
use Stevenyangecho\UEditor\Uploader\UploadScrawl;

class ToolsController extends Controller
{
    //上传图片
    public function setUploadsImg(Request $request)
    {
        $file = $request->file('image');
        if (empty($file)) {
            $return = [
                'status' => 201,
                'msg' => '请选择图片上传'
            ];
        } else {
            $path = date('Y-m-d');
            $public = $request->file('image')->store($path, 'oss');
            if ($public) {
                $res = Storage::put($public, Storage::disk('oss')->get($public));
                if ($res) {
                    $return = [
                        'status' => 200,
                        'filename' => $public,
                        'msg' => "https://imgs.jianjiangedu.cn/" . $public,
                    ];
                } else {
                    $return = [
                        'status' => 200,
                        'message' => trans('上传成功'),
                        'filename' => $public,
                        'msg' => Storage::disk('oss')->url($public)
                    ];
                }
            } else {
                $return = [
                    'status_code' => 201,
                    'msg' => trans('上传失败')
                ];
            }
        }
        return $return;
    }

    public function uploads(Request $request)
    {
        $file = $request->file('image');
        if (empty($file)) {
            return showMsg('请选择图片上传', 201);
        } else {
            $path = '/uploads/' . date('Y-m-d');
            $public = $request->file('image')->store($path, 'oss');
            if ($public) {
                $res = Storage::put($public, Storage::disk('oss')->get($public));
                if ($res) {
                    $img_url = $public;
                    $oss_url = getAliOssImage($public);
                    return showMsg('添加成功', 200, ['img_url' => $img_url, 'oss_url' => $oss_url]);
                } else {
                    return showMsg('上传失败', 201);
                }
            } else {
                return showMsg('上传失败', 201);
            }
        }
    }

    public function imageUpload(Request $request)
    {
        $file = $request->file('image');
        $category_id = $request->get('category_id');
        if (empty($file)) {
            return showMsg('请选择图片上传', 201);
        } else {
            $path = date('Y-m-d');
            $public = $request->file('image')->store($path, 'oss');
            if ($public) {
                $res = Storage::put($public, Storage::disk('oss')->get($public));
                if ($res) {
                    $img_url = config('app.url') . '/uploads/' . $public;
                    $ary['uId'] = 0;
                    $ary['category_id'] = $category_id;
                    $ary['photo'] = $img_url;
                    Photos::create($ary);
                    return showMsg('添加成功', 200, ['img_url' => $img_url]);
                } else {
                    return showMsg('上传失败', 201);
                }
            } else {
                return showMsg('上传失败', 201);
            }
        }
    }

    public function videoUpload(Request $request)
    {
        $file = $request->file('image');
        if (empty($file)) {
            return showMsg('请选择图片上传', 201);
        } else {
            $path = date('Y-m-d');
            $public = $request->file('image')->store($path, 'article');
            if ($public) {
                $res = Storage::put($public, Storage::disk('article')->get($public));
                if ($res) {
                    $img_url = config('app.url') . '/uploads/' . $public;
                    return showMsg('添加成功', 200, ['img_url' => $img_url]);
                } else {
                    return showMsg('上传失败', 201);
                }
            } else {
                return showMsg('上传失败', 201);
            }
        }
    }

    public function server(Request $request)
    {
        $config = config('UEditorUpload.upload');
        $action = $request->get('action');
        switch ($action) {
            case 'config':
                $result = $config;
                break;
            case 'uploadimage':
                $upConfig = array(
                    "pathFormat" => $config['imagePathFormat'],
                    "maxSize" => $config['imageMaxSize'],
                    "allowFiles" => $config['imageAllowFiles'],
                    'fieldName' => $config['imageFieldName'],
                );
                //$result = with(new UploadFile($upConfig, $request))->upload();

                $path = date('Y-m-d');
                $public = $request->file('upfile')->store($path, 'oss');
                $res = Storage::put($public, Storage::disk('oss')->get($public));
                if ($res) {
                    //$img_url = config('app.url') . '/uploads/' . $public;
                    $result['original'] = "";
                    $result['size'] = "";
                    $result['state'] = "SUCCESS";
                    $result['title'] = "";
                    $result['type'] = "";
                    $result['url'] = getAliOssImage($public);
                }
                break;
            case 'uploadscrawl':
                $upConfig = array(
                    "pathFormat" => $config['scrawlPathFormat'],
                    "maxSize" => $config['scrawlMaxSize'],
                    //   "allowFiles" => $config['scrawlAllowFiles'],
                    "oriName" => "scrawl.png",
                    'fieldName' => $config['scrawlFieldName'],
                );
                $result = with(new UploadScrawl($upConfig, $request))->upload();

                break;
            case 'uploadvideo':
                $upConfig = array(
                    "pathFormat" => $config['videoPathFormat'],
                    "maxSize" => $config['videoMaxSize'],
                    "allowFiles" => $config['videoAllowFiles'],
                    'fieldName' => $config['videoFieldName'],
                );
                $result = with(new UploadFile($upConfig, $request))->upload();

                break;
            case 'uploadfile':
            default:
                $upConfig = array(
                    "pathFormat" => $config['filePathFormat'],
                    "maxSize" => $config['fileMaxSize'],
                    "allowFiles" => $config['fileAllowFiles'],
                    'fieldName' => $config['fileFieldName'],
                );
                $result = with(new UploadFile($upConfig, $request))->upload();

                break;

            /* 列出图片 */
            case 'listimage':


                if (config('UEditorUpload.core.mode') == 'local') {
                    $result = with(new Lists(
                        $config['imageManagerAllowFiles'],
                        $config['imageManagerListSize'],
                        $config['imageManagerListPath'],
                        $request))->getList();
                } else if (config('UEditorUpload.core.mode') == 'qiniu') {
                    $result = with(new ListsQiniu(
                        $config['imageManagerAllowFiles'],
                        $config['imageManagerListSize'],
                        $config['imageManagerListPath'],
                        $request))->getList();
                } else {
                    $result = with(new Lists(
                        $config['imageManagerAllowFiles'],
                        $config['imageManagerListSize'],
                        $config['imageManagerListPath'],
                        $request))->getList();
                }


                break;
            /* 列出文件 */
            case 'listfile':
                if (config('UEditorUpload.core.mode') == 'local') {
                    $result = with(new Lists(
                        $config['fileManagerAllowFiles'],
                        $config['fileManagerListSize'],
                        $config['fileManagerListPath'],
                        $request))->getList();
                } else if (config('UEditorUpload.core.mode') == 'qiniu') {
                    $result = with(new ListsQiniu(
                        $config['fileManagerAllowFiles'],
                        $config['fileManagerListSize'],
                        $config['fileManagerListPath'],
                        $request))->getList();
                } else {
                    $result = with(new Lists(
                        $config['imageManagerAllowFiles'],
                        $config['imageManagerListSize'],
                        $config['imageManagerListPath'],
                        $request))->getList();
                }

                break;

            /* 抓取远程文件 */
            case 'catchimage':

                $upConfig = array(
                    "pathFormat" => $config['catcherPathFormat'],
                    "maxSize" => $config['catcherMaxSize'],
                    "allowFiles" => $config['catcherAllowFiles'],
                    "oriName" => "remote.png",
                    'fieldName' => $config['catcherFieldName'],
                );

                $sources = $request->get($upConfig['fieldName']);
                $list = [];
                foreach ($sources as $imgUrl) {
                    $upConfig['imgUrl'] = $imgUrl;
                    $info = with(new UploadCatch($upConfig, $request))->upload();

                    array_push($list, array(
                        "state" => $info["state"],
                        "url" => $info["url"],
                        "size" => $info["size"],
                        "title" => htmlspecialchars($info["title"]),
                        "original" => htmlspecialchars($info["original"]),
                        "source" => htmlspecialchars($imgUrl)
                    ));
                }
                $result = [
                    'state' => count($list) ? 'SUCCESS' : 'ERROR',
                    'list' => $list
                ];
                break;
        }

        if (strpos($_SERVER['HTTP_USER_AGENT'], "Triden")) {
            //如果是IE 特殊处理header
            return response($result, 200)->header('Content-Type', 'text/html;charset=utf-8');
        } else {
            return response()->json($result, 200, [], JSON_UNESCAPED_UNICODE);
        }

    }

}
